﻿package TaskStructure
{
	import MyDisplayObjects.MyButton;
	
	import flash.display.*;
	import flash.events.*;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	public class IdScreen extends Sprite
	{
		public static const TEXT_COLOR:uint = 0x0000FF, TEXT_BACKGROUND_COLOR:uint = 0xEEEEFF;
		public var projectID:String;
		public var problemNumber:int;
		private var ldr:Loader;
		private var readyToContinue:Boolean = false;
		private var clickSquare:MyButton;
		private var firstNameTField:TextField, lastNameTField:TextField, projectTField:TextField, problemNumberTField:TextField;
		private var ifirstNameTField:TextField, ilastNameTField:TextField, iprojectTField:TextField, iproblemNumberTField:TextField;
		private var feedbackTField:TextField;
		
		public function IdScreen()
		{
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			this.graphics.beginFill(0xFFDDDD);
			this.graphics.drawRect(0,0, parent.width, parent.height);
			this.graphics.endFill();
			ldr = new Loader();
			var urlReq:URLRequest = new URLRequest(String("Taskstructure/opening.swf"));
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadedImage);
			ldr.load(urlReq);
		}
		private function handleLoaderError (event:IOErrorEvent):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			this.dispatchEvent( new Event(Event.CANCEL));
			drawTextFields();
		}
		/** Once the image has been loaded, display it */
		private function handleLoadedImage (event:Event):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			this.addChild(ldr.content);
			ldr.x = (this.width - ldr.width);
			ldr.y = (this.height - ldr.height);
			drawTextFields();
		}
		private function drawTextFields():void
		{
			var fontSize:int = Math.floor(this.height/30);
			//var fontSize:int = 20;
			// Add textfield to get user id,  add textfield to provide this information
			firstNameTField = new TextField();
			firstNameTField.background = true;
			firstNameTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			firstNameTField.multiline = false;
			firstNameTField.type = TextFieldType.INPUT;
			firstNameTField.backgroundColor = TEXT_BACKGROUND_COLOR;
			firstNameTField.border = true;
			firstNameTField.width = fontSize*10;
			firstNameTField.height = fontSize * 1.5;
			firstNameTField.x = this.stage.stageWidth/4;
			firstNameTField.y = this.stage.stageHeight/2 + firstNameTField.height;
			this.addChild(firstNameTField);
				
			lastNameTField = new TextField();
			lastNameTField.background = true;
			lastNameTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			lastNameTField.multiline = false;
			lastNameTField.type = TextFieldType.INPUT;
			lastNameTField.backgroundColor = TEXT_BACKGROUND_COLOR;
			lastNameTField.border = true;
			lastNameTField.width = fontSize*10;
			lastNameTField.height = fontSize * 1.5;			
			lastNameTField.x = this.stage.stageWidth - this.stage.stageWidth/4 - lastNameTField.width;
			lastNameTField.y = this.stage.stageHeight/2 + firstNameTField.height;
			this.addChild(lastNameTField);
			
			projectTField = new TextField();
			projectTField.background = true;
			projectTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			projectTField.multiline = false;
			projectTField.type = TextFieldType.INPUT;
			projectTField.backgroundColor = TEXT_BACKGROUND_COLOR;
			projectTField.border = true;
			projectTField.width = fontSize*10;
			projectTField.height = fontSize * 1.5;
			projectTField.x = this.stage.stageWidth/4;
			projectTField.y = this.stage.stageHeight*3/4;
			this.addChild(projectTField);
			
			problemNumberTField = new TextField();
			problemNumberTField.background = true;
			problemNumberTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			problemNumberTField.multiline = false;
			problemNumberTField.type = TextFieldType.INPUT;
			problemNumberTField.backgroundColor = TEXT_BACKGROUND_COLOR;
			problemNumberTField.border = true;
			problemNumberTField.width = fontSize * 2;
			problemNumberTField.height = fontSize * 1.5;
			problemNumberTField.text = "1";
			problemNumberTField.maxChars = 2;
			problemNumberTField.restrict = "0-9";				
			problemNumberTField.x = this.stage.stageWidth - this.stage.stageWidth/4 - lastNameTField.width;
			problemNumberTField.y = this.stage.stageHeight*3/4;
			this.addChild(problemNumberTField);			
			
			// instructions above each textfield
			ifirstNameTField = new TextField();
			ifirstNameTField.selectable = false;
			ifirstNameTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			ifirstNameTField.selectable = false;
			ifirstNameTField.text = "First name";
			ifirstNameTField.autoSize = "left";
			ifirstNameTField.x = firstNameTField.x + (firstNameTField.width - ifirstNameTField.width)/2;
			ifirstNameTField.y = firstNameTField.y - ifirstNameTField.height;
			this.addChild(ifirstNameTField);
			
			ilastNameTField = new TextField();
			ilastNameTField.selectable = false;
			ilastNameTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			ilastNameTField.selectable = false;
			ilastNameTField.text = "Last name";
			ilastNameTField.autoSize = "left";
			ilastNameTField.x = lastNameTField.x + (lastNameTField.width - ilastNameTField.width)/2;
			ilastNameTField.y = lastNameTField.y - ilastNameTField.height;
			this.addChild(ilastNameTField);
			
			iprojectTField = new TextField();
			iprojectTField.selectable = false;
			iprojectTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			iprojectTField.selectable = false;
			iprojectTField.text = "Project name";
			iprojectTField.autoSize = "left";
			iprojectTField.x = projectTField.x + (projectTField.width - iprojectTField.width)/2;
			iprojectTField.y = projectTField.y - iprojectTField.height;
			this.addChild(iprojectTField);
			
			iproblemNumberTField = new TextField();
			iproblemNumberTField.selectable = false;
			iproblemNumberTField.defaultTextFormat = new TextFormat("Arial", fontSize, TEXT_COLOR);
			iproblemNumberTField.selectable = false;
			iproblemNumberTField.text = "Problem #";
			iproblemNumberTField.autoSize = "left";
			iproblemNumberTField.x = problemNumberTField.x + (problemNumberTField.width - iproblemNumberTField.width)/2;
			iproblemNumberTField.y = problemNumberTField.y - iproblemNumberTField.height;
			this.addChild(iproblemNumberTField);
						
			clickSquare = new MyButton(this.width/8, fontSize*2, fontSize*2/10, fontSize*2/10, "Ready!", 0x008800, 0, false);
			clickSquare.addEventListener(MouseEvent.CLICK, mousePressHandler);
			clickSquare.y = this.stage.stageHeight - 2*clickSquare.height;
			clickSquare.x = (this.stage.stageWidth - clickSquare.width)/2;
			this.addChild(clickSquare);
			
			feedbackTField = new TextField();
			feedbackTField.selectable = false;
			feedbackTField.defaultTextFormat = new TextFormat("Arial", fontSize, 0x880000);
			feedbackTField.selectable = false;
			this.addChild(feedbackTField);		
		}
		
		/** Integrates first and last name */
		public function get userID():String
		{
			var string:String = ""
			if (firstNameTField.text != ""){
				string = firstNameTField.text.toLowerCase();
			} 
			if (lastNameTField.text != ""){
				string = string+lastNameTField.text.toUpperCase();
			}  
			return string;
		}
		
		
		/** When user presses the button we must check whether the userID is filled out and the labels contain either even or odd */
		private function mousePressHandler(event:MouseEvent):void
		{
			if (firstNameTField.text.length > 0)
			{		
				projectID = projectTField.text;
				problemNumber = int(problemNumberTField.text);
				this.dispatchEvent(new Event(Event.COMPLETE));
			} else
			{
				feedbackTField.text = "Please enter your name.";
				feedbackTField.autoSize = "left";
				feedbackTField.x = firstNameTField.x;
				feedbackTField.y = firstNameTField.y + firstNameTField.height;
				firstNameTField.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOverFirstName);
			}
		}
			private function handleMouseOverFirstName (event:MouseEvent):void
			{
				firstNameTField.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOverFirstName);	
				feedbackTField.text = "";
			}
			
		public static function removeWhitespace(_str:String):String{
			var string:String = "";
			for (var i:int=0; i < _str.length; i++){
				if (_str.charAt(i) != " "){
					string = string + _str.charAt(i);
				}
			}
			return string;
		}
				
	}
}